<?php
/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return
 *   A string containing the breadcrumb output.
 */
function balo360com_breadcrumb($variables){
  $breadcrumb = $variables['breadcrumb'];
  $breadcrumb_separator = theme_get_setting('breadcrumb_separator','balo360com');

  $show_breadcrumb_home = theme_get_setting('breadcrumb_home');
  if (!$show_breadcrumb_home) {
    array_shift($breadcrumb);
  }

  if (!empty($breadcrumb)) {
    $breadcrumb[] = drupal_get_title();
    return '<div class="breadcrumb">' . implode(' <span class="breadcrumb-separator">' . $breadcrumb_separator . '</span>', $breadcrumb) . '</div>';
  }
}

/**
 * Implements of hook_page_alter()
 */
function balo360com_page_alter($page) {
	if (theme_get_setting('responsive_meta','balo360com')):
	$mobileoptimized = array(
		'#type' => 'html_tag',
		'#tag' => 'meta',
		'#attributes' => array(
  		'name' =>  'MobileOptimized',
  		'content' =>  'width'
		)
	);

	$handheldfriendly = array(
		'#type' => 'html_tag',
		'#tag' => 'meta',
		'#attributes' => array(
  		'name' =>  'HandheldFriendly',
  		'content' =>  'true'
		)
	);

	$viewport = array(
		'#type' => 'html_tag',
		'#tag' => 'meta',
		'#attributes' => array(
		'name' =>  'viewport',
		'content' =>  'width=device-width, initial-scale=1'
		)
	);
	
	drupal_add_html_head($mobileoptimized, 'MobileOptimized');
	drupal_add_html_head($handheldfriendly, 'HandheldFriendly');
	drupal_add_html_head($viewport, 'viewport');
	endif;
  /**
   * Add Javascript for responsive mobile menu
   */
  if (theme_get_setting('responsive_menu_state','balo360com')) {

    $responsive_menu_switchwidth=theme_get_setting('responsive_menu_switchwidth','balo360com');
    $responsive_menu_topoptiontext=theme_get_setting('responsive_menu_topoptiontext','balo360com');

    drupal_add_js(drupal_get_path('theme', 'balo360com') .'/js/jquery.mobilemenu.min.js');
    drupal_add_js('
      jQuery(document).ready(function($) {
        $("#navigation > ul.menu, #navigation ul.sf-menu, #navigation .content > ul.menu").mobileMenu({
        prependTo: "#navigation",
        combine: false,
        switchWidth: '.$responsive_menu_switchwidth.',
        topOptionText: "'.$responsive_menu_topoptiontext.'"
        });
      });', array('type' => 'inline', 'scope' => 'footer', 'weight' => 10)
    );
  }
}

function balo360com_preprocess_html(&$variables) {
	if (!theme_get_setting('responsive_respond','balo360com')):
	drupal_add_css(path_to_theme() . '/css/basic-layout.css', array('group' => CSS_THEME, 'browsers' => array('IE' => '(lte IE 8)&(!IEMobile)', '!IE' => FALSE), 'preprocess' => FALSE));
	endif;
	
	drupal_add_css(path_to_theme() . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => '(lte IE 8)&(!IEMobile)', '!IE' => FALSE), 'preprocess' => FALSE));
}

/**
 * Override or insert variables into the html template.
 */
function balo360com_process_html(&$vars) {
  // Rebuild the script and page_bottom
  $vars['page_bottom'] .= drupal_get_js('extra_footer');
}

function balo360com_preprocess_node(&$build) {
  if ($build['type'] == 'product') {
    $build['content']['title'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('product-title')),
      '#weight' => $build['content']['field_images']['#weight'] + 1,
      'title' => array('#markup' => $build['title'])
    );
    $build['content']['group_product_price'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('group-product-price')),
      '#weight' => $build['content']['title']['#weight'] + 1
    );
    $build['content']['group_product_price']['product:commerce_price'] = $build['content']['product:commerce_price'];
    $build['content']['group_product_price']['field_product'] = $build['content']['field_product'];
    hide($build['content']['product:commerce_price']);
    hide($build['content']['field_product']);
  }
}

/**
 * Override or insert variables into the page template.
 */
function balo360com_process_page(&$variables) {
  if ($variables['is_front']) {
    if (theme_get_setting('hide_frontpage_main_content')) {
      $variables['page']['content']['system_main'] = array();
    }
  }
  // Always print the site name and slogan, but if they are toggled off, we'll
  // just hide them visually.
  $variables['hide_site_name']   = theme_get_setting('toggle_name') ? FALSE : TRUE;
  $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE;
  if ($variables['hide_site_name']) {
    // If toggle_name is FALSE, the site_name will be empty, so we rebuild it.
    $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  }
  if ($variables['hide_site_slogan']) {
    // If toggle_site_slogan is FALSE, the site_slogan will be empty, so we rebuild it.
    $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
  }
  //build breadcrumb for specific pages
  if (!empty($variables['node']->field_catalog[LANGUAGE_NONE][0]['tid'])) {
    $breadcrumbs = array();
    $tid = $variables['node']->field_catalog[LANGUAGE_NONE][0]['tid'];
    $term = taxonomy_term_load($tid);

    $breadcrumb_separator = theme_get_setting('breadcrumb_separator','balo360com');
    $breadcrumbs[] = l(t('Homepage'), '<front>');
    if ($term) {
      $breadcrumbs[] = l(t('Products'), 'products');
      $breadcrumbs[] = l($term->name, 'taxonomy/term/' . $term->tid);
    }
    $breadcrumbs[] = $variables['title'];

    $show_breadcrumb_home = theme_get_setting('breadcrumb_home');
    if (!$show_breadcrumb_home) {
      array_shift($breadcrumbs);
    }

    $variables['breadcrumb'] = '<div class="breadcrumb">' . implode(' <span class="breadcrumb-separator">' . $breadcrumb_separator . '</span>', $breadcrumbs) . '</div>';
  }
}

function balo360com_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    unset($form['search_block_form']['#title']);
    $form['search_block_form']['#title_display'] = 'invisible';
	  $form_default = t('Search');
    $form['search_block_form']['#default_value'] = $form_default;
    $form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/search-button.png');

 	  $form['search_block_form']['#attributes'] = array('onblur' => "if (this.value == '') {this.value = '{$form_default}';}", 'onfocus' => "if (this.value == '{$form_default}') {this.value = '';}" );
  }
}

/**
 * Implement of hook_js_alter
 */
function balo360com_js_alter(&$js) {
  foreach ($js as $name => $javascript) {
    // Move footer to really below the header scope
    if ($js[$name]['scope'] == 'footer') {
      $js[$name]['scope'] = 'extra_footer';
    }

    $exclude = FALSE;
    $preprocess = TRUE;

    // Ckeditor doesn't work in compress mode
    if (is_string($js[$name]['data']) && strstr($js[$name]['data'], 'ckeditor.js') && $js[$name]['type'] != 'inline') {
      $preprocess = FALSE;
    }

    // Add to any need to be exclude
    if ($js[$name]['type']== 'inline' && is_string($js[$name]['data']) && strstr($js[$name]['data'], 'data')) {
      $exclude = TRUE;
    }

    if (!$exclude) {
      // Change the weight with the scope -> group -> every_page -> weight Ref: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7
      $js[$name]['weight'] = ($js[$name]['scope'] != 'header') * 10000
        + $js[$name]['group'] * 10  // $js[$name]['group'] is -100/0/100
        + (!$js[$name]['every_page']) * 100
        + $js[$name]['weight'];

      $js[$name]['scope'] = $js[$name]['scope'] != 'extra_footer' ? 'footer' : 'extra_footer';
      $js[$name]['group'] = JS_DEFAULT;
      $js[$name]['every_page'] = TRUE;
      $js[$name]['preprocess'] = $preprocess;
    }
  }
}

/**
 * Aggregate and compress CSS files
 */
function balo360com_css_alter(&$css) {
  // Sort CSS items, so that they appear in the correct order.
  // This is taken from drupal_get_css().
  uasort($css, 'drupal_sort_css_js');

  // The other style sheets
  // I will populate this array with the other than "All" css (usually I have only one but just in case�)
  $other = array();

  // I will add some weight to the new $css array so every element keeps its position
  $weight = 0;

  foreach ($css as $name => $style) {
    $css[$name]['group'] = CSS_DEFAULT;
    $css[$name]['preprocess'] = TRUE;
    $css[$name]['every_page'] = TRUE;
    // I move all the style sheets other than all to a new array
    if ($css[$name]['media'] != 'all' && $css[$name]['media'] != 'screen') {
      // remove and add to a new array
      $other[$css[$name]['media']][$name] = $css[$name];
      unset($css[$name]);
      continue;
    }
    // I leave untouched the conditional stylesheets
    // and put all the rest inside a 0 group
    if ($css[$name]['browsers']['!IE']) {
      $css[$name]['weight'] = ++$weight;
      $css[$name]['media'] != 'all';
    }
  }

  // I merge the regular array and the other css array
  foreach ($other as $key => $val) {
    //rearrange weight continuous in group
    foreach ($val as &$v) {
      $v['weight'] = ++$weight;
    }
    //arranged by weight css array
    $css = array_merge($css, $val);
  }
}